<?php
class Renew_Model_BukkenTenpo extends Apm_Model_Base {

  public function getTenpoList($code) {
    $sql = "select T03_TENPO_CD,T03_TENPO_NAME,T03_HEYA_CD,
                   T03_KASHIDASHI_KUBUN,T03_FREERENT_FLAG,T03_TEN_FREERENT_FLAG,T03_HEYAWARI_FLAG,T03_TEN_HEYAWARI_FLAG,T03_DAIHYO_FLG,
                   M05_TENPO_TEL01,M05_JYUUSHO_TEXT,M05_TENPO_BANCHI,M05_TENPO_JYUUSHO_BIKOU,
                   M05_TENPO_BANGOU,M05_EIGYOU_ZIKAN_OPEN_H,M05_EIGYOU_ZIKAN_OPEN_M,
                   M05_EIGYOU_ZIKAN_CLOSE_H,M05_EIGYOU_ZIKAN_CLOSE_M,
                   M05_KOUTSUU_TEXT01,M05_TOHO01,
                   M05_TEIKYUUBI,M05_TEIKYUUBI_BIKOU,T03_SHIKIKIN,T03_SHIKIKIN_TSUKISUU,T03_REIKIN,T03_REIKIN_TSUKISUU,
                   T03_TORIHIKI_TAIYOU,T03_HOSHOUKIN,T03_HOSHOUKIN_TSUKISUU,
                   M05_MENKYO_HAKKOUMOTO,M05_MENKYO_KOUSHINKAI,M05_MENKYO_GOUSUU";
	for ($call_no=1; $call_no<=Apm_Config_Shop::CALLNOTE_NO_MAX; $call_no++) {
		$sql .= ",M93.M93_CONNECTION_NO".$call_no;
		$sql .= ",M93.M93_CONTROL_FLAG".$call_no;
	}
	$sql .= ",M93.M93_USE_KBN,M93.M93_START_DATE,M93.M93_DEL_DATE";
	$sql .= " from T03WEB_HEYA,M05TENPO_INFO,M93CALLNOTE M93
              where T03_KOUKAI_FLAG=1
                and T03_TENPO_CD=M05_TENPO_CD
                and T03_DAIHYO_HEYA_CD=".$code."
                and T03_TENPO_CD = M93.M93_TENPO_CD(+)
                group by T03_TENPO_CD,T03_TENPO_NAME,T03_HEYA_CD,
                         T03_KASHIDASHI_KUBUN,T03_FREERENT_FLAG,T03_TEN_FREERENT_FLAG,T03_HEYAWARI_FLAG,T03_TEN_HEYAWARI_FLAG,T03_DAIHYO_FLG,
                         M05_TENPO_TEL01,M05_JYUUSHO_TEXT,M05_TENPO_BANCHI,M05_TENPO_JYUUSHO_BIKOU,
                         M05_TENPO_BANGOU,M05_EIGYOU_ZIKAN_OPEN_H,M05_EIGYOU_ZIKAN_OPEN_M,
                   M05_EIGYOU_ZIKAN_CLOSE_H,M05_EIGYOU_ZIKAN_CLOSE_M,
                   M05_KOUTSUU_TEXT01,M05_TOHO01,
                   M05_TEIKYUUBI,M05_TEIKYUUBI_BIKOU,T03_SHIKIKIN,T03_SHIKIKIN_TSUKISUU,T03_REIKIN,T03_REIKIN_TSUKISUU,
                   T03_TORIHIKI_TAIYOU,T03_HOSHOUKIN,T03_HOSHOUKIN_TSUKISUU,
                   M05_MENKYO_HAKKOUMOTO,M05_MENKYO_KOUSHINKAI,M05_MENKYO_GOUSUU";
	for ($call_no=1; $call_no<=Apm_Config_Shop::CALLNOTE_NO_MAX; $call_no++) {
		$sql .= ",M93.M93_CONNECTION_NO".$call_no;
		$sql .= ",M93.M93_CONTROL_FLAG".$call_no;
	}
	$sql .= ",M93.M93_USE_KBN,M93.M93_START_DATE,M93.M93_DEL_DATE";
    $sql .= "   order by min(T03_TENPO_HYOJI_YUSEN)";
    $list = $this->dbGetList($sql);

    $model_cam = new Renew_Model_BukkenCampaign();
    $norm_cam = $model_cam->getNormalCampaigns();

    $img_base = 'http://img.apamanshop.com/fs/tenpoimg/';
    foreach ($list as $i => $tenpo) {
      $cd = $tenpo['T03_TENPO_CD'];
      $list[$i]['STUFF_IMG'] = $img_base.'s'.Apm_ShopUtil::getOldTenpoCd($cd).'.jpg';
      $list[$i]['GAIKAN_IMG'] = $img_base.'g'.Apm_ShopUtil::getOldTenpoCd($cd).'.jpg';
      $list[$i]['TEIKYUUBI'] = $this->convertTeikyuubi($tenpo);
      $list[$i]['SHIKIKIN'] = Apm_Bukkenitems::GetShikikinStr($tenpo);
      $list[$i]['REIKIN'] = Apm_Bukkenitems::GetReikinStr($tenpo);
      $list[$i]['HOSHOUKIN'] = Apm_Bukkenitems::GetHoshoukinStr($tenpo);

      $list[$i]['LINE_URL'] = Apm_Config_Shop::URL_LINE;
      $list[$i]['M05_TENPO_LINE_ID'] = Apm_ShopUtil::getTenpoLineId($cd);

      $heya_cam = $model_cam->getHeyaCampaigns($tenpo['T03_HEYA_CD']);
      $list[$i]['CAMPAIGNS'] = array_merge($heya_cam,$norm_cam);

    }
    return $list;
  }


  public function getTenpoListHeya($code) {
    $sql = "select T03_TENPO_CD,T03_TENPO_NAME,T03_HEYA_CD,
                   T03_KASHIDASHI_KUBUN,T03_FREERENT_FLAG,T03_TEN_FREERENT_FLAG,T03_HEYAWARI_FLAG,T03_TEN_HEYAWARI_FLAG,T03_DAIHYO_FLG,
                   M05_TENPO_TEL01,M05_JYUUSHO_TEXT,M05_TENPO_BANCHI,M05_TENPO_JYUUSHO_BIKOU,
                   M05_TENPO_BANGOU,M05_EIGYOU_ZIKAN_OPEN_H,M05_EIGYOU_ZIKAN_OPEN_M,
                   M05_EIGYOU_ZIKAN_CLOSE_H,M05_EIGYOU_ZIKAN_CLOSE_M,
                   M05_KOUTSUU_TEXT01,M05_TOHO01,
                   M05_TEIKYUUBI,M05_TEIKYUUBI_BIKOU,T03_SHIKIKIN,T03_SHIKIKIN_TSUKISUU,T03_REIKIN,T03_REIKIN_TSUKISUU,
                   T03_TORIHIKI_TAIYOU,T03_HOSHOUKIN,T03_HOSHOUKIN_TSUKISUU,
                   M05_MENKYO_HAKKOUMOTO,M05_MENKYO_KOUSHINKAI,M05_MENKYO_GOUSUU";
	for ($call_no=1; $call_no<=Apm_Config_Shop::CALLNOTE_NO_MAX; $call_no++) {
		$sql .= ",M93.M93_CONNECTION_NO".$call_no;
		$sql .= ",M93.M93_CONTROL_FLAG".$call_no;
	}
	$sql .= ",M93.M93_USE_KBN,M93.M93_START_DATE,M93.M93_DEL_DATE";
	$sql .= " from T03WEB_HEYA,M05TENPO_INFO,M93CALLNOTE M93
              where T03_KOUKAI_FLAG=1
                and T03_TENPO_CD=M05_TENPO_CD
                and T03_HEYA_CD=".$code."
                and T03_TENPO_CD = M93.M93_TENPO_CD(+)
                group by T03_TENPO_CD,T03_TENPO_NAME,T03_HEYA_CD,
                         T03_KASHIDASHI_KUBUN,T03_FREERENT_FLAG,T03_TEN_FREERENT_FLAG,T03_HEYAWARI_FLAG,T03_TEN_HEYAWARI_FLAG,T03_DAIHYO_FLG,
                         M05_TENPO_TEL01,M05_JYUUSHO_TEXT,M05_TENPO_BANCHI,M05_TENPO_JYUUSHO_BIKOU,
                         M05_TENPO_BANGOU,M05_EIGYOU_ZIKAN_OPEN_H,M05_EIGYOU_ZIKAN_OPEN_M,
                   M05_EIGYOU_ZIKAN_CLOSE_H,M05_EIGYOU_ZIKAN_CLOSE_M,
                   M05_KOUTSUU_TEXT01,M05_TOHO01,
                   M05_TEIKYUUBI,M05_TEIKYUUBI_BIKOU,T03_SHIKIKIN,T03_SHIKIKIN_TSUKISUU,T03_REIKIN,T03_REIKIN_TSUKISUU,
                   T03_TORIHIKI_TAIYOU,T03_HOSHOUKIN,T03_HOSHOUKIN_TSUKISUU,
                   M05_MENKYO_HAKKOUMOTO,M05_MENKYO_KOUSHINKAI,M05_MENKYO_GOUSUU";
	for ($call_no=1; $call_no<=Apm_Config_Shop::CALLNOTE_NO_MAX; $call_no++) {
		$sql .= ",M93.M93_CONNECTION_NO".$call_no;
		$sql .= ",M93.M93_CONTROL_FLAG".$call_no;
	}
	$sql .= ",M93.M93_USE_KBN,M93.M93_START_DATE,M93.M93_DEL_DATE";
    $sql .= "   order by min(T03_TENPO_HYOJI_YUSEN)";
    $list = $this->dbGetList($sql);

    $model_cam = new Renew_Model_BukkenCampaign();
    $norm_cam = $model_cam->getNormalCampaigns();

    $img_base = 'http://img.apamanshop.com/fs/tenpoimg/';
    foreach ($list as $i => $tenpo) {
      $cd = $tenpo['T03_TENPO_CD'];
      $list[$i]['STUFF_IMG'] = $img_base.'s'.Apm_ShopUtil::getOldTenpoCd($cd).'.jpg';
      $list[$i]['GAIKAN_IMG'] = $img_base.'g'.Apm_ShopUtil::getOldTenpoCd($cd).'.jpg';
      $list[$i]['TEIKYUUBI'] = $this->convertTeikyuubi($tenpo);
      $list[$i]['SHIKIKIN'] = Apm_Bukkenitems::GetShikikinStr($tenpo);
      $list[$i]['REIKIN'] = Apm_Bukkenitems::GetReikinStr($tenpo);
      $list[$i]['HOSHOUKIN'] = Apm_Bukkenitems::GetHoshoukinStr($tenpo);

      $list[$i]['LINE_URL'] = Apm_Config_Shop::URL_LINE;
      $list[$i]['M05_TENPO_LINE_ID'] = Apm_ShopUtil::getTenpoLineId($cd);

      $heya_cam = $model_cam->getHeyaCampaigns($tenpo['T03_HEYA_CD']);
      $list[$i]['CAMPAIGNS'] = array_merge($heya_cam,$norm_cam);

    }
    return $list;
  }



   private function convertTeikyuubi($data)
  {
    $teikyubi = $data['M05_TEIKYUUBI'];
    //$teikyubiBikou = $data['M05_TEIKYUUBI_BIKOU'];
    $teikyubiName = '';

    $youbi = array('月','火','水','木','金','土','日');
    
    if ($teikyubi == '0000000' && $teikyubiBikou == '')
      {
	$teikyubiName = '-';
      }
    else
      {
	$array = array();
	for ($i=0; $i<7; $i++) {
	  if (substr($teikyubi, $i, 1) == '1') 
	    $array[] = mb_convert_encoding($youbi[$i].'曜日','SJIS','UTF-8');
	}
	//if ($teikyubiBikou != '') $array[] = $teikyubiBikou;
	$teikyubiName = join(" ", $array);
      }
    return $teikyubiName;
  }
}
